package model;
import controller.MessageController;
import controller.RoomController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Room {

    private HashMap<CommonContent.DIRECTION, Room> neighbor = new HashMap<>();

    public void setRoom(CommonContent.DIRECTION d, Room r) {//将新房间设定在隔壁，可用于建新图
        neighbor.put(d, r);
        // assert r.getRoom(d) == this;
    }

    private String roomDescription;//房间介绍
    private String roomLooking;
    private String roomId;
    private String roomName;
    private HashMap<String, Player> playerList = new HashMap<>();//<id,player>
    private HashMap<String, Npc> npcList = new HashMap<>();
    private HashMap<String,Monster> monsterList=new HashMap<>();
    private HashMap<String,Item> itemList =new HashMap<>();


    public Room getDes(CommonContent.DIRECTION d) {
        return neighbor.get(d);
    }

    public void exit(Player player, CommonContent.DIRECTION direction) throws IOException {//将玩家从房间移除
        removePlayer(player);
        Room r = getDes(direction);
        for(Player p : playerList.values()) {
            MessageController.showToPlayer(p,player.getName()+"朝着"+direction+"的"+r.roomName+"离开了。");
        }
    }

    public void enter(Player player, CommonContent.DIRECTION direction) throws IOException {
        Room r = getDes(direction);
        for(Player p : playerList.values()) {
            MessageController.showToPlayer(p,player.getName()+"从"+direction+"的"+r.roomName+"来到此地。");
        }
        addPlayer(player);
    }

    public void enter(Player player) throws IOException {
        for (Player p : playerList.values()) {
            MessageController.showToPlayer(p,player.getName()+"从混沌之地来到此地");
        }
        addPlayer(player);
    }

    public void removePlayer(Player player) throws IOException {
        playerList.remove(player.getName());
        MessageController.showToPlayer(player,"您已离开。\n\n");
    }
    public void addPlayer(Player player) throws IOException {
        playerList.put(player.getName(), player);
        MessageController.showToPlayer(player,getRoomLooking());
    }

    public void setDescription(String roomDescription) {
        this.roomDescription = roomDescription;
    }

    public void setRoomId(String roomId) {
        this.roomId = roomId;
    }

    public String getRoomId() {
        return roomId;
    }

    public void SetRoomName(String roomName) {
        this.roomName = roomName;
    }

    public String getRoomName() {
        return roomName;
    }

    public String getRoomLooking() {

        roomLooking = roomName + "\t";

        int roomDescriptionLength = roomDescription.length();
        if (roomDescriptionLength <= CommonContent.CHARS_PER_LINE)
            roomLooking += roomDescription + "\t";
        else {
            int i;
            for (i = 0; i <= roomDescriptionLength
                    - CommonContent.CHARS_PER_LINE; i = i
                    + CommonContent.CHARS_PER_LINE) {
                roomLooking += roomDescription.substring(i, i
                        + CommonContent.CHARS_PER_LINE)
                        + "\t";
            }
            roomLooking += roomDescription.substring(i, roomDescriptionLength)
                    + "\t";
        }

        // �������
        roomLooking += getChuKou() + "\t";
        // ����npc

        // ����player
        roomLooking += listRoomPlayers();
        roomLooking+=listRoomNpcs();
        roomLooking+=listMonsters();
        roomLooking+=listItems();
        return roomLooking;
    }
    private String listRoomPlayers(){
        String temp = "在此地的玩家有:\n";
        for(Player p : playerList.values()) {
            temp += p.getName()+"\t";
        }
        return temp;
    }
    private String listRoomNpcs(){
        String temp = "在此地的Npc有:\n";
        for(Npc npc : npcList.values()) {
            temp += npc.getName()+"\t";
        }
        return temp;
    }
    private String listMonsters(){
        String temp = "在此地的敌人有:\n";
        for(Monster m : monsterList.values()) {
            temp += m.getName()+"\t";
        }
        return temp;
    }
    private String listItems(){
        String temp = "在此地的物品有:\n";
        for(Item item : itemList.values()) {
            temp += item.getName()+"x"+item.getNum()+"\t";
        }
        return temp;
    }
    private String getChuKou() {

        String temp = "由此地可前往:\n";
        for(Map.Entry<CommonContent.DIRECTION,Room> entry : neighbor.entrySet()) {
            temp += entry.getKey()+"  "+entry.getValue().getRoomName()+"\n";
        }
        return temp;
    }
    public boolean have(String object) {
        return itemList.containsKey(object);
    }

    public Item getItem(String object) {
        return itemList.get(object);
    }

    public void NpcLeave(Npc npc) throws IOException {
        Room to= RoomController.getRoom(npc.moveRandomly());
        npcList.remove(npc.getName());
        to.NpcEnter(npc);
        for(Player p : playerList.values()) {
            MessageController.showToPlayer(p,npc.getName()+"前往了"+to.getRoomName()+"。");
        }
    }
    public void NpcEnter(Npc npc) throws IOException {
        npcList.put(npc.getName(), npc);
        for(Player p : playerList.values()) {
            MessageController.showToPlayer(p,npc.getName()+"来到此地。");
        }
    }
    public void NpcMove() throws IOException {
        for(Npc npc : npcList.values()) {
            if(npc.getMovecooldown()<=0){
                NpcLeave(npc);
            }else {
                npc.delMovecooldown();
            }
        }
    }
    public boolean playerExist(String playerName) {
        return playerList.containsKey(playerName);
    }
    public Player getPlayer(String playerName) {
        return playerList.get(playerName);
    }

    public boolean npcExist()  {
        return npcExist(null);
    }

    public boolean npcExist(String s)   {
        return npcList.containsKey(s);
    }
    public Npc getNpc(String s)  {
        return npcList.get(s);
    }
    public boolean monsterExists(String s)  {
        return monsterList.containsKey(s);
    }
    public Monster getMonster(String s)  {
        return monsterList.get(s);
    }
    public void itemSet(Item item) {
        if (itemList.containsKey(item.getName())) {
            Item i = itemList.get(item.getName());
            i.setNum(i.getNum()+item.getNum());
        } else{
            itemList.put(item.getName(), item);
        }
    }
    public void itemgot(Item item) {
        itemList.remove(item.getName());
    }
    public void mosterSet(Monster monster) {
        if(playerList.isEmpty()&&!monsterList.containsKey(monster.getName())) {
            monsterList.put(monster.getName(), monster);
        }
    }
    public void mosterRemove(String s) {
        monsterList.remove(s);
    }
}
